package ATMproject;

import com.sun.source.tree.NewArrayTree;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Transform extends JPanel implements ActionListener {

    private JButton backBtn;

    private MainPage frame;

    private Icon backButton = Util.getIcon("/atmimage/buttonOff.jpg");
    private Icon transformButton = Util.getIcon("/atmimage/buttonSure.jpg");

    //声明转账金额和转账对象
    private JTextField transformMoney = new JTextField();
    private JTextField transformPerson = new JTextField();
    private JButton transformBtn = new JButton(transformButton);
    private JLabel personLabel = new JLabel("请输入转账对象：");
    private JLabel moneyLabel = new JLabel("请输入转账金额：");


    public Transform(MainPage frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

//        JLabel titleLabel = new JLabel("转账");
//        titleLabel.setBounds(200, 50, 100, 50);
//        this.add(titleLabel);

        backBtn = new JButton(backButton);
        backBtn.setBounds(650, 150, 100, 50);
        this.add(backBtn);
        backBtn.addActionListener(this);

        transformPerson.setBounds(300, 150, 200, 25);
        this.add(transformPerson);

        transformMoney.setBounds(300, 200, 200, 25);
        this.add(transformMoney);

        transformBtn.setBounds(350, 330, 100, 50);
        this.add(transformBtn);
        transformBtn.addActionListener(this);

        personLabel.setBounds(170, 150, 150, 25);
        this.add(personLabel);

        moneyLabel.setBounds(170, 200, 150, 25);
        this.add(moneyLabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (backBtn == e.getSource()) {
            frame.switchPanel("menu");
            return;
        }

//        if (transformPerson.getText().equals(frame.getUserarraylist().get(frame.getIndex()).getAccount())) {
//            JOptionPane.showMessageDialog(this, "不能给自己转账");
//        } else {
//            for (int i = 0; i < frame.getUserarraylist().size(); i++) {
//                if (transformPerson.getText().equals(frame.getUserarraylist().get(i).getAccount()) && Double.valueOf(transformMoney.getText()) < frame.getUserarraylist().get(frame.getIndex()).getMoney()) {
//                    double outMoney = frame.getUserarraylist().get(frame.getIndex()).getMoney() - Double.valueOf(transformMoney.getText());
//                    frame.getUserarraylist().get(frame.getIndex()).setMoney(outMoney);
//                    JOptionPane.showMessageDialog(this, outMoney);
//                    double inMoney = frame.getUserarraylist().get(i).getMoney() + Double.valueOf(transformMoney.getText());
//                    frame.getUserarraylist().get(i).setMoney(inMoney);
//                    JOptionPane.showMessageDialog(this, inMoney);
//                }
//            }
//            JOptionPane.showMessageDialog(this, "找不到收款方");
//        }

        if (transformPerson.getText().equals(frame.getUser().getAccount())) {
            JOptionPane.showMessageDialog(this, "不能转给自己");
            return;
        }
        if (frame.getUser().getMoney() < Double.valueOf(transformMoney.getText())) {
            JOptionPane.showMessageDialog(this, "余额不足");
            return;
        }
//        System.out.println(frame.getUser().getMoney());
//        System.out.println(Integer.parseInt(transformMoney.getText()));
        for (int i = 0; i < frame.getUserarraylist().size(); i++) {
            if (transformPerson.getText().equals(frame.getUserarraylist().get(i).getAccount())) {
                double outMoney = frame.getUser().getMoney() - Double.valueOf(transformMoney.getText());
                frame.getUserarraylist().get(frame.getIndex()).setMoney(outMoney);
                JOptionPane.showMessageDialog(this, "您当前余额为：" + outMoney);
                double inMoney = Double.valueOf(transformMoney.getText()) + frame.getUserarraylist().get(i).getMoney();
                frame.getUserarraylist().get(i).setMoney(inMoney);
                transformPerson.setText("");
                transformMoney.setText("");
                JOptionPane.showMessageDialog(this, "对方余额为：" + inMoney);
                return;
            }
        }
        JOptionPane.showMessageDialog(this, "查无此人");


    }
}
